/****************************************************************************
 * Copyright 2010 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.greatq.conditions;

import com.gotobject.filter.common.Predicates;
import com.gotobject.filter.predicate.AbstractPredicate;
import com.gotobject.filter.predicate.Predicate;
import com.gotobject.filter.spi.ExecutionContext;
import com.gotobject.greatq.Query;

import static com.gotobject.filter.common.Predicates.not;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class PredicateMaker {
    private PredicateMaker(){}

    public static Predicate<Object> allQueries() {
        return Predicates.all();
    }

    public static Predicate<Query> ending(final String with){
        return ExecutionContext.any(new AbstractPredicate<Query>(){
            @Override
            public boolean apply(Query query) {
                final String key = query.getTag();
                return key != null && (key.endsWith(with));
            }

            @Override
            public String toString() {
                return "ending(" + with + ")";
            }
        });
    }
    
    public static Predicate<Query> like(final String value){
        return ExecutionContext.any(new AbstractPredicate<Query>(){
            @Override
            public boolean apply(Query query) {
                final String key = query.getTag();
                return key != null && (key.contains(value) || key.startsWith(value) || key.endsWith(value));
            }

            @Override
            public String toString() {
                return "like(" + value + ")";
            }
        });
    }

    public static Predicate<Query> eq(final String value){
        return ExecutionContext.any(new AbstractPredicate<Query>(){
            @Override
            public boolean apply(Query query) {
                final String key = query.getTag();
                return key != null && (key.equalsIgnoreCase(value));
            }

            @Override
            public String toString() {
                return "eq(" + value + ")";
            }
        });
    }

    public static Predicate<Query> onlyBatch() {
        return ExecutionContext.any(new AbstractPredicate<Query>(){
            @Override
            public boolean apply(Query query) {
                return query.inBatchMode();
            }

            @Override
            public String toString() {
                return "onlyBatch()";
            }
        });
    }

    public static Predicate<Query> onlyExecuted() {
        return ExecutionContext.any(new AbstractPredicate<Query>(){
            @Override
            public boolean apply(Query query) {
                return query.isExecuted();
            }

            @Override
            public String toString() {
                return "onlyExecuted()";
            }
        });
    }

    public static Predicate<Query> onlyNotExecuted(){
        return ExecutionContext.any(not(onlyExecuted()));
    }

    public static Predicate<Query> onlySingle(){
        final Predicate<Query> single = not(onlyBatch());
        return ExecutionContext.any(new AbstractPredicate<Query>(){
            @Override
            public boolean apply(Query query) {
                return single.apply(query);
            }

            @Override
            public String toString() {
                return "onlySingle()";
            }
        });
    }
}
